﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Admin 的摘要说明
/// </summary>
public class Admin
{
    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    private string adminname;

    public string Adminname
    {
        get { return adminname; }
        set { adminname = value; }
    }
    private string adminpwd;

    public string Adminpwd
    {
        get { return adminpwd; }
        set { adminpwd = value; }
    }

    public const string ADDADMIN = "insert into admin values(@adminname,@adminpwd)";

    public const string DELADMIN = "delete from admin where id=@id";

    public const string GETALLADMIN = "select * from admin";

    public const string UPDATEADMIN = "update admin set @adminpwd=@adminpwd where id=@id";

    public const string VERIFYADMIN = "select count(*) from admin where adminname=@adminname and adminpwd=@adminpwd";

    public const string GETADMINBYCHECK = "select * from admin where adminname=@adminname and adminpwd=@adminpwd";

    public void AddAdmin(Admin a)
    {
        SqlHelper.ExecuteScalar(ADDADMIN,
            new SqlParameter("@adminname", a.Adminname),
            new SqlParameter("@adminpwd", a.Adminpwd));
    }

    public void UpdateAdmin(Admin a)
    {
        SqlHelper.ExecuteScalar(UPDATEADMIN,
            new SqlParameter("@adminpwd", a.Adminpwd),
            new SqlParameter("@id", a.Id));
    }

    public int GetAdminByCheck(string adminname, string adminpwd) 
    {
        DataTable dt = new DataTable();
        SqlHelper.FillDataTable(GETADMINBYCHECK, dt, 
            new SqlParameter("@adminname", adminname),
            new SqlParameter("@adminpwd", adminpwd));
        return int.Parse(dt.Rows[0]["id"].ToString());
    }


    public DataTable GetAllAdmin()
    {
        DataTable dt = new DataTable();
        SqlHelper.FillDataTable(GETALLADMIN, dt);
        return dt;
    }

    public void DelAdmin(int id)
    {
        SqlHelper.ExecuteScalar(DELADMIN,
            new SqlParameter("@id", id));
    }

    public int VerifyAdmin(string adminname, string adminpwd)
    {
        return int.Parse(SqlHelper.ExecuteScalar(VERIFYADMIN,
            new SqlParameter("@adminname", adminname),
            new SqlParameter("@adminpwd", adminpwd)).ToString());
    }

}
